import Actor from "@/Actor";
import ActorUtil from "@/ActorUtil";
import ActorItem from "@/ActorItem";
import ActorAbility from "@/ActorAbility";
import InputUtil from "@/InputUtil";
import ItemUtil from "@/ItemUtil";
import AbilityButtonUtil from "@/AbilityButtonUtil";

export default class MessageUtil {

    static hookEventCallBacks: {
        [type: string]: ((msg: any) => boolean)[]
    } = {}


    private static _sl_isInitialized = false

    // private static _sl_lastReleaseItem = null


    private static _sl_onHookMsg(msg: any): boolean {
        let type = msg.type;
        if (type == null) {
            return true
        }
        if (type == "key_down") {
            // MessageUtil._sl_lastReleaseItem = null
        } else if (type == "mouse_ability") {//鼠标点击技能
            if (msg.ability == null) {
                return true
            }
            let actor: ActorAbility = Actor.getByTemplateId(id2string(msg.ability));
            if (actor != null) {
                let b = actor.localClick(msg);
                if (b == false) {
                    return false;
                }
                if (actor.isPassive() || actor.isDisable() || actor.isHide()) {
                    return false
                }
            }
        } else if (type == "mouse_item") {
            //点击技能时 msg.ability 为0
            let order: number = msg.order;
            let itemIndex = order - 852008
            // if (MessageUtil._sl_lastReleaseItem != null) {
            //     MessageUtil._sl_lastReleaseItem = null
            //     return true
            // }
            // if (msg.code == 4) {
            //     MessageUtil._sl_lastReleaseItem = itemIndex
            //     return true;
            // }
            let item = UnitItemInSlot(selection(), itemIndex);
            let actor: ActorItem = Actor.getByTemplateId(id2string(GetItemTypeId(item)));
            if (actor != null) {
                let b = actor.localClick(msg);
                if (b == false) {
                    return false;
                }
                if (actor.isPassive() || actor.isDisable() || actor.isHide()) {
                    return false
                }
            }
        }

        let hookEventCallBacks = MessageUtil.hookEventCallBacks[type];
        if (hookEventCallBacks == null) {
            return true;
        }
        for (let callBack of hookEventCallBacks) {
            let f = callBack(msg);
            if (f == false) {
                return f
            }
        }
        return true;
    }

    static _sl_init_message_hook() {
        if (MessageUtil._sl_isInitialized) {
            return;
        }
        MessageUtil._sl_isInitialized = true;
        //
        const message: NoSelf = require('jass.message')
        let hook: (this: void, msg) => boolean = message.hook;
        if (hook == null) {
            message.hook = function (msg: any) {
                return MessageUtil._sl_onHookMsg(msg);
            }
        } else {
            message.hook = function (this: void, msg: any) {
                let flag = MessageUtil._sl_onHookMsg(msg);
                if (flag == false) {
                    //返回fasle 表示拦截
                    return flag;
                }
                return hook(msg);
            }
        }
        //内置才有的效果
        _G["WindowEventCallBack"] = function (this: void, eventId: number) {
            if (eventId == 1) {//鼠标按下
                let sceneX = InputUtil.getMouseSceneX()
                let sceneY = InputUtil.getMouseSceneY()
                // let itemSlot = ItemUtil.getItemSlotBySceneXY(sceneX, sceneY);
                // if (itemSlot != null) {
                //     if (MessageUtil._sl_lastReleaseItem != null) {
                //         return false
                //     }
                //     let item = UnitItemInSlot(selection(), itemSlot);
                //     let actor: ActorItem = Actor.getByTemplateId(id2string(GetItemTypeId(item)));
                //     if (actor != null) {
                //         if (actor.isPassive() || actor.isDisable() || actor.isHide()) {
                //             return true
                //         }
                //     }
                // }
                let xyObj = AbilityButtonUtil.getPosBySceneXY(sceneX, sceneY);
                if (xyObj != null) {
                    const [abilityId] = button(xyObj.x, xyObj.y);
                    if (abilityId != null) {
                        let actor: ActorAbility = Actor.getByTemplateId(id2string(abilityId));
                        if (actor != null) {
                            if (actor.isPassive() || actor.isDisable() || actor.isHide()) {
                                return true
                            }
                        }
                    }
                }
            }

            //返回fasle 表示不拦截
            return false
        }
        //

    }

}